package com.team3.ecommerce.controller;

import com.team3.ecommerce.domain.CommInOrder;
import com.team3.ecommerce.domain.Commodity;
import com.team3.ecommerce.domain.Order;
import com.team3.ecommerce.domain.UserInfo;
import com.team3.ecommerce.service.CommodityService;
import com.team3.ecommerce.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/commodity")
public class CommodityController {
@Autowired
    private CommodityService commodityService;
@Autowired
private OrderService orderService;

    @RequestMapping("/Details/{id}")
    public String commodityDetails(Model model, @PathVariable("id") String id){
        Commodity commodity = commodityService.findCommodityById(id);
        model.addAttribute("commodity",commodity);

        return "commodityDetails.html";
    }

    @RequestMapping("/Search")
    public String commoditySearch(Model model,@RequestParam(value = "searchString") String searchString,@RequestParam(value = "commType") String commType,
                                  @RequestParam(value = "commStock",required = false) String commStock,
                                  @RequestParam(value = "priceLowerBound",required = false) String priceLowerBound,
                                  @RequestParam(value = "priceUpperBound",required = false) String priceUpperBound){
        if (commStock == null || priceLowerBound == null || priceUpperBound == null) {
            if (commType.equals("所有")) {
                List<Commodity> commodities = commodityService.searchCommodity(searchString);
                model.addAttribute("commodities", commodities);
                model.addAttribute("searchString", searchString);
                model.addAttribute("commType",commType);
            } else {
                List<Commodity> commodities = commodityService.searchCommodityTypeLimited(searchString, commType);
                model.addAttribute("commodities", commodities);
                model.addAttribute("searchString", searchString);
                model.addAttribute("commType",commType);
            }
        }
        else {
//            if (commType.equals("所有")) {
//                List<Commodity> commodities = commodityService.searchCommodity(searchString);
//                model.addAttribute("commodities", commodities);
//                model.addAttribute("searchString", searchString);
//
//            } else {
//                List<Commodity> commodities = commodityService.searchCommodityTypeLimited(searchString, commType);
//                model.addAttribute("commodities", commodities);
//                model.addAttribute("searchString", searchString);
//                model.addAttribute("commType",commType);
//            }
            List<Commodity> commodities = commodityService.commodityFilter(searchString,commType,commStock,Integer.parseInt(priceLowerBound),Integer.parseInt(priceUpperBound));
            model.addAttribute("commodities", commodities);
            model.addAttribute("searchString", searchString);
            model.addAttribute("commType",commType);
        }
        return "commoditySearchResult.html";
    }

//    @RequestMapping("/Filter")
//    public String commodityFilter(Model model,@RequestParam(value = "searchString") String searchString,@RequestParam(value = "commType") String commType,@RequestParam(value = "commStock") String commStock,@RequestParam(value = "priceLowerBound") int priceLowerBound,@RequestParam(value = "priceUpperBound") int priceUpperBound){
//        List<Commodity> commodities = commodityService.commodityFilter(searchString,commType,commStock,priceLowerBound,priceUpperBound);
//        model.addAttribute("commodities",commodities);
//        model.addAttribute("searchString",searchString);
//        return "commoditySearchResult.html";
//    }


//    @RequestMapping("/commToOrder/{commId}")
//    public String commToOrder(Model model,@PathVariable("commId") String commId){
//        Order order = orderService.newOrderByButtom(commId);
//        List<CommInOrder> commInOrderList = orderService.getCommInOrderlist(commId);
//        List<Commodity> commodityList = new ArrayList<Commodity>();
//        commodityList.clear();
//        for (int i = 0; i < commInOrderList.size();i++){
//            Commodity com = commodityService.findCommodityById(commInOrderList.get(i).getCommUid());
//            commodityList.add(com);
//        }
//
//        model.addAttribute("order",order);
//        model.addAttribute("commodityList",commodityList);
//        model.addAttribute("commInOrderList",commInOrderList);
//        return "orderDetails.html";
//    }
}
